package com.seo.proxy;

import java.nio.ByteBuffer;

public class Buffer {

	private byte[] buffer;

	private int position;
	
	public Buffer(int size) {
		this.buffer = new byte[size];
		this.position = 0;
	}

	public byte[] getBuffer() {
		return buffer;
	}

	public void setBuffer(byte[] buffer) {
		this.buffer = buffer;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public boolean append(ByteBuffer byteBuffer) {
		int size = byteBuffer.position();
		if (position + size > buffer.length) {
			return false;
		}
		byteBuffer.rewind();
		byteBuffer.get(buffer, position, size);
		position += size;
		return true;
	}

	public String getString(int start, int length) {
		return new String(buffer, start, length);
	}

	public void compact(int parsePosition) {
		if (position != parsePosition) {
			System.arraycopy(buffer, parsePosition, buffer, 0, position - parsePosition);
		}
		position -= parsePosition;
		parsePosition = 0;
	}
}
